﻿using ConfExpert.Forms.Base;
using System;
namespace ConfExpert.Forms.Depo.Gecici
{
    public partial class Toplar : XForm
    {
        private void Toplar_Load(object sender, EventArgs e)
        {
        }
        /*

        public Toplar()
        {
            InitializeComponent();
            this.barcode_read += new barcode_readHandler(BarkodOkuEkle_barcode_read);
            txtRollId.Properties.ReadOnly = true;
            txtRollId.DoubleClick += new EventHandler(txtRollId_DoubleClick);
            txtRollId.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.BarkodOkuEkle_KeyPress);
        }

        void txtRollId_DoubleClick(object sender, EventArgs e)
        {
            barCode = string.Empty;
        }
        
        string barCode = string.Empty;
        void BarkodOkuEkle_barcode_read(string barcode)
        {
            //txtRollId.Text += barCode + Environment.NewLine;
            int RollId = intParse(barCode.Substring(0, barCode.Length-1));

            int RowHandle = gvRoll.LocateByDisplayText(0, gvRoll.Columns["RollId"], RollId.ToString());

            if (RowHandle < 0)
            {

                MyData.xToplar2Row r = ds.xToplar2.NewxToplar2Row();
                r.Tarih = DateTime.Now;
                r.RollId = RollId;
                ds.xToplar2.AddxToplar2Row(r);
                RowHandle = gvRoll.LocateByDisplayText(0, gvRoll.Columns["RollId"], RollId.ToString());
            }

            gvRoll.FocusedRowHandle = RowHandle;
            barCode = string.Empty;
        }

        delegate void barcode_readHandler(string barcode);
        event barcode_readHandler barcode_read;

        private void BarkodOkuEkle_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool isDigit = Char.IsDigit(e.KeyChar);
            if (isDigit)
            {
                barCode += e.KeyChar.ToString();
                if (barCode.Length == 8)
                {
                    barcode_read(barCode);
                    txtRollId.SelectionStart = 0;   // txtRollId.Text.Length;
                    
                }
            }
        }


        private void Toplar_Load(object sender, EventArgs e)
        {
            bs = new BindingSource(ds, "xToplar2");
            InitGridView(gvRoll, bs);
            //gvRoll.Columns["Tarih"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            gvRoll.Columns["Tarih"].DisplayFormat.FormatString = "g";

            GetDetails("xToplar2", "GetRollPhotos");
            gvRoll.BestFitColumns();

            //dtToplar = new DataTable("Toplar");
            //DataColumn cTarih = new DataColumn("Tarih", typeof(DateTime));
            //DataColumn cEpc = new DataColumn("RollId", typeof(int));
            //DataColumn cPhoto = new DataColumn("Photo", typeof(Image));
            //dtToplar.Columns.AddRange(new DataColumn[] {
            //    cTarih, cEpc, cPhoto
            //});
            //ds.Tables.Add(dtToplar);
            //gcRoll.DataSource = bs;

            gvRoll.OptionsView.EnableAppearanceEvenRow = false;
            gvRoll.OptionsBehavior.Editable = false;
            gvRoll.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(gvRoll_RowStyle);
            bs.CurrentChanged += new EventHandler(bs_CurrentChanged);

            this.Shown += new EventHandler(Toplar_Shown);
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            btnTara.ItemClick += btnTara_ItemClick;
        }

        void btnTara_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Scan();
        }

        void gvRoll_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (gvRoll.FocusedRowHandle == e.RowHandle)
                if (e.RowHandle > -1)
                    e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;
        }

        void bs_CurrentChanged(object sender, EventArgs e)
        {
            RollId = intParse(bs, "RollId");
            txtInfo.Caption = "RollId- " + RollId.ToString();
        }

        void Toplar_Shown(object sender, EventArgs e)
        {
            txtRollId.Focus();
        }


        //int TopId = 0;
        BindingSource bs;

        void Scan()
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                
                //Scanner scanner = new Scanner();
                Image i2 = Scanner.Scan();    //kumaş için sabitlendi

                if (i2 != null)
                {
                    RollId = intParse(bs, "RollId");
                    if (ds.xPhotoR.FindByRollId(RollId) == null)
                    {
                        MyData.xPhotoRRow xpr = ds.xPhotoR.NewxPhotoRRow();
                        xpr.RollId = RollId;
                        ds.xPhotoR.AddxPhotoRRow(xpr);
                    }

                    MyData.xPhotoRRow r = ds.xPhotoR.FindByRollId(RollId);
                    r.Photo = ConvertImageToByteArray(i2, System.Drawing.Imaging.ImageFormat.Png);


                    
                    //r.Hue = (decimal)((Color)GetDominantColor(i2 as Bitmap)).GetHue();
                    //r.Bri = (decimal)((Color)GetDominantColor(i2 as Bitmap)).GetBrightness();
                    //r.Sat = (decimal)((Color)GetDominantColor(i2 as Bitmap)).GetSaturation();
                    //r.R = (int)((Color)GetDominantColor(i2 as Bitmap)).R;
                    //r.G = (int)((Color)GetDominantColor(i2 as Bitmap)).G;
                    //r.B = (int)((Color)GetDominantColor(i2 as Bitmap)).B;

                    //gvRoll.SetFocusedRowCellValue("Photo", ConvertImageToByteArray(Resources.icon_time, System.Drawing.Imaging.ImageFormat.Png));
                    //gvRoll.SetFocusedRowCellValue("Hue", (decimal)((Color)GetDominantColor(i2 as Bitmap)).GetHue());
                    //gvRoll.SetFocusedRowCellValue("Bri", (decimal)((Color)GetDominantColor(i2 as Bitmap)).GetBrightness());
                    //gvRoll.SetFocusedRowCellValue("Sat", (decimal)((Color)GetDominantColor(i2 as Bitmap)).GetSaturation());
                    //gvRoll.SetFocusedRowCellValue("R", (int)((Color)GetDominantColor(i2 as Bitmap)).R);
                    //gvRoll.SetFocusedRowCellValue("G", (int)((Color)GetDominantColor(i2 as Bitmap)).G);
                    //gvRoll.SetFocusedRowCellValue("B", (int)((Color)GetDominantColor(i2 as Bitmap)).B);
                    
                    gvRoll.BestFitColumns();
                    //gvRoll.Columns["Photo"].Width = 34;
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

        }

        int RollId = 0;
        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                btnKaydet.Enabled = false;
                //TODO: Orjinal resim burada resize edilip gönderilecek.                
                ExecuteNonQuery("UpdPhotoR", ds.xPhotoR);
                ds.xPhotoR.Clear();
                GC.Collect();
                Notify("Kaydedildi");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                btnKaydet.Enabled = true;

                txtRollId.Focus();
                this.Cursor = Cursors.Default;
            }
        }
        */
    }
}